﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM;
using SAPbobsCOM;
using System.Text.RegularExpressions;

namespace BoAddonBiz_FreightForwarder
{
    [BoAddonBiz_FreightForwarder.BusinessBase.ControlB1Form(IsNew = true, FormType = B1FormTypes.ado_AVA_FF_Pickup)]
    public class B1Form_Pickup : B1Form_BaseOperationSon
    {
        #region public properties
        public override string ThisSubTableName
        {
            get { return null; }
        }
        public override string ThisSubTableName2
        {
            get { return null; }
        }
        public override string FatherField
        {
            get { return "U_IsPickup"; }
        }

        public override string FatherUDS
        {
            get { return "OPUP_UID"; }
        }

        public override string ThisTableName
        {
            get { return "@AVA_FF_OPUP"; }
        }

        public override string ThisObjectCode
        {
            get { return "AVA_FF_Pickup"; }
        }
        #endregion

        #region constructor
        public B1Form_Pickup()
            : base()
        {
            this.frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_Pickup_frm_ChooseFormListEvent);
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_Pickup_frm_ItemEvent);
        }

        void B1Form_Pickup_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.Before_Action)
            {
            }
            else
            {
                if (pVal.ItemUID == "1000007" && pVal.EventType == BoEventTypes.et_COMBO_SELECT)
                {
                    SelectCargo();
                }
            }
        }
        #endregion

        #region events
        void B1Form_Pickup_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            if (pVal.Before_Action)
            {
                ////不做限制（2011-06-01 10:58:22 by 旭旭）
                //return;
                SAPbouiCOM.ChooseFromList cfl = this.CurrentForm.ChooseFromLists.Item(pVal.ChooseFromListUID);
                SAPbouiCOM.Conditions cons = default (SAPbouiCOM.Conditions);
                SAPbouiCOM.Condition con=default (SAPbouiCOM.Condition);
                switch (pVal.ChooseFromListUID)
                {
                    #region 不需要的过滤，先注释掉下一步删除 modified by WangPeng 2012-6-19
                    //case "CFL_OCRD1"://委托方
                    //    cons = new SAPbouiCOM.Conditions();
                    //    con = cons.Add();
                    //    con.Alias = "QryGroup13";
                    //    con.CondVal = "Y";
                    //    con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                    //    cfl.SetConditions(cons);
                    //    break;
                    #endregion
                    case "CFL_OCRD3":/*发货单位*/
                    case "CFL_OCRD4":/*接货单位*/
                        cons = new SAPbouiCOM.Conditions();
                        con = cons.Add();
                        con.Alias = "QryGroup5";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup7";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup11";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup12";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_OR;
                        con = cons.Add();
                        con.Alias = "QryGroup13";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                    case "CFL_OCPR_Issue":/*发货联系人*/
                        Utils.ChooseFromListAddress_Filter(this.CurrentForm, ref cfl, ThisTableName, "U_PickupCode", cons, con, ref  BubbleEvent);
                        break;
                    case "CFL_OCPR_Rec":/*接货联系人*/
                        Utils.ChooseFromListAddress_Filter(this.CurrentForm, ref cfl, ThisTableName, "U_ConsigneeCode", cons, con, ref  BubbleEvent);
                        break;
                }
            }
            else
            {
                if (pVal.Action_Success)
                {
                    dynamic Customerinfo;
                    SAPbouiCOM.DBDataSource dbds = this.CurrentForm.DataSources.DBDataSources.Item(ThisTableName);
                    if (pVal.ChooseFromListUID == "CFL_OCRD1")//客户
                    {
                        dbds.SetValue("U_CardCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_CardName", dbds.Offset, pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                    }
                    else if (pVal.ChooseFromListUID == "CFL_OCRD4")//接货公司
                    {
                        dbds.SetValue("U_ConsigneeCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_ConsigneeName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardFName", 0).ToString() : pVal.SelectedObjects.GetValue("CardName", 0).ToString());

                        Customerinfo = Utils.GetDefaultContractPerson(SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_ConsigneeAddress", dbds.Offset, Customerinfo.Address);
                    }
                    else if (pVal.ChooseFromListUID == "CFL_OCRD3")//提货地址
                    {
                        dbds.SetValue("U_PickupCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_PickupName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardFName", 0).ToString() : pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        Customerinfo = Utils.GetDefaultContractPerson(SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_PickupAddress", dbds.Offset, Customerinfo.Address);
                    }
                    else if (pVal.ChooseFromListUID == "CFL_OCRD2")//运输供应商
                    {
                        dbds.SetValue("U_ConsignorCode", dbds.Offset, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_ConsignorName", dbds.Offset, string.IsNullOrEmpty(pVal.SelectedObjects.GetValue("CardName", 0).ToString()) ? pVal.SelectedObjects.GetValue("CardFName", 0).ToString() : pVal.SelectedObjects.GetValue("CardName", 0).ToString());
                        Customerinfo = Utils.GetDefaultContractPerson(SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        dbds.SetValue("U_ConsignorCntct", dbds.Offset, Customerinfo.FullName);

                    }
                    else if (pVal.ChooseFromListUID == "CFL_OCPR_Issue")/*发货联系人*/
                    {
                        dbds = this.CurrentForm.DataSources.DBDataSources.Item(ThisTableName);
                        dbds.SetValue("U_PickupAddress",dbds.Offset, Utils.GetAddress(pVal));
                    }
                    else if (pVal.ChooseFromListUID == "CFL_OCPR_Rec")/*接货联系人*/
                    {
                        dbds.SetValue("U_ConsigneeAddress", dbds.Offset, Utils.GetAddress(pVal));
                    }
                }
            }
        }
        #endregion

        #region override methods
        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_FF_Pickup;
            FormSetting.FormFileName = "AVA_FF_PickupForm.srf";
            base.FormInfoSetting(FormSetting);
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            Utils.InitialItemComboBox(this.SBOCompany, this.CurrentForm.Items.Item("95"), "Code", "Name", "@AVA_FF_OPCT", "Code", null, null, true);//包装方式

            base.InitializeForm(InitPar);
            this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_Weight", 0, "0");
            //this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_ConsignDate", 0, DateTime.Now.ToString("yyyyMMdd")); /*Commented by WangPeng 2012-6-27*/
            //总重量，总体积，包装形式等，需要从表头代入一次。并添加件数，尺寸和总计费重字段，也从表头代入数据。
            Form listForm = SBOApp.Forms.Item(InitPar.FatherFormUID.Split(';')[0]);
            Form BNOForm = SBOApp.Forms.Item(listForm.DataSources.UserDataSources.Item("ParentUID").Value);

            if (!String.IsNullOrEmpty(InitPar.FatherFormUID))
            {
                if (this.CurrentForm.Mode == BoFormMode.fm_ADD_MODE)
                {
                    DBDataSource dbds_obno = BNOForm.DataSources.DBDataSources.Item("@AVA_FF_OBNO");
                    DBDataSource main = this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName);
                }
            }
            ComboBox cb = this.CurrentForm.Items.Item("1000007").Specific;
            for (int i = 0; i < BNOForm.DataSources.DBDataSources.Item("@AVA_FF_BNO1").Size; i++)
            {
                cb.ValidValues.Add(BNOForm.DataSources.DBDataSources.Item("@AVA_FF_BNO1").GetValue("LineId", i).Trim(), Regex.Split(BNOForm.DataSources.DBDataSources.Item("@AVA_FF_BNO1").GetValue("U_CargoName", i).Trim(), "\r")[0]);
            }
            if (cb.ValidValues.Count > 0)
            {
                cb.Select(0, BoSearchKey.psk_Index);
                SelectCargo();
            }
        }
        #endregion

        private void SelectCargo()
        {
            ComboBox cb = this.CurrentForm.Items.Item("1000007").Specific;
            string lineId = cb.Value.Trim();
            this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_BaseLine", 0, lineId);
            Form listForm = SBOApp.Forms.Item(this.CurrentForm.DataSources.UserDataSources.Item("ParentUID").Value);
            Form BNOForm = SBOApp.Forms.Item(listForm.DataSources.UserDataSources.Item("ParentUID").Value);
            DBDataSource dbds_bno1 = BNOForm.DataSources.DBDataSources.Item("@AVA_FF_BNO1");
            for (int i = 0; i < dbds_bno1.Size; i++)
            {
                if (dbds_bno1.GetValue("LineId", i).Trim() == lineId)
                {
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_CargoSize", 0, dbds_bno1.GetValue("U_CargoSize", i));
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_CargoQuantity", 0, dbds_bno1.GetValue("U_Quantity", i));
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_TotalVolumn", 0, dbds_bno1.GetValue("U_Volume", i));
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_EstimateGross", 0, dbds_bno1.GetValue("U_GrossWeight", i));
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_Weight", 0, dbds_bno1.GetValue("U_BillingWeight", i));
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_PackagedForm", 0, dbds_bno1.GetValue("U_PackForm", i));
                    this.CurrentForm.DataSources.DBDataSources.Item(this.ThisTableName).SetValue("U_CargoType", 0, dbds_bno1.GetValue("U_CargoType", i));
                    break;
                }
            }
        }

    }
}
